package com.x.mvp.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.x.mvp.R;

/**
 * Created by H on 17/11/2.
 */

public class AlartDialog extends Dialog {
    ImageView icon;
    TextView messageTv;
    public static final int TYPE_SUCCESS = 1;
    public static final int TYPE_FIAL = 2;
    public static final int TYPE_ALART = 3;

    private static final int MESSAGE_TIMEOUT = 2;

    private WorkerHandler mHandler;
    private boolean isShow = false;
    int time = 0;
    IDissmiss iDissmiss;


    public AlartDialog(Context context) {
        super(context, R.style.Custom_Progress);
        mHandler = new WorkerHandler();
        createView(context);

    }

    public AlartDialog(Context context, int time) {
        super(context, R.style.Custom_Progress);
        mHandler = new WorkerHandler();
        this.time = time;
        createView(context);
    }

    private AlartDialog setListener(boolean cancelable,
                                    OnCancelListener cancelListener) {
        setCancelable(cancelable);
        setOnCancelListener(cancelListener);
        getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.dimAmount = 0.2f;
        getWindow().setAttributes(lp);
        return this;
    }

    public void showMessage(int type, String message, int time) {
        icon.setTag(type);
        switch (type) {
            case TYPE_ALART:
                icon.setImageResource(R.drawable.icon_alert);
                break;
            case TYPE_FIAL:
                icon.setImageResource(R.drawable.icon_fail);
                break;
            case TYPE_SUCCESS:
                icon.setImageResource(R.drawable.icon_success);
                break;
        }

        messageTv.setText(message);
        show(time);

    }

    public void show(int time) {
        if (time == 0) {
            show();
        } else {
            show();
            mHandler.sendEmptyMessageDelayed(MESSAGE_TIMEOUT, (long) (time * 1000));
        }
    }

    protected void createView(Context context) {
        setTitle("");
        setContentView(R.layout.dialog_alert_box);
        icon = (ImageView) findViewById(R.id.icon);
        messageTv = (TextView) findViewById(R.id.message);
    }

    private class WorkerHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_TIMEOUT:
                    dismiss();
                    if (iDissmiss != null) {
                        iDissmiss.onDismiss((Integer) icon.getTag());
                    }
                    break;
            }
        }
    }

    public void setiDissmiss(IDissmiss iDissmiss) {
        this.iDissmiss = iDissmiss;
    }

    public interface IDissmiss {
        public void onDismiss(int type);
    }
}
